﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OneMile.Entity.Model;
using System.Data.SqlClient;
using System.Data;
using OneMile.Common;

namespace OneMile.DAL
{
   public  class DALSOrder:BaseDAL
    {
       public IList<EOrder> GetTodayOrderList(Guid ShopId)
       {
           String sql = @"SELECT OrderId,UserId,OrderAddress,OrderMobile,TotalPrice,EarliestTime,LatestTime,OrderName,OrderType,OrderNumber
                            FROM Fct_Order 
                           WHERE ShopId=@shopid AND (Rec_CreateTime between @starttime AND @endtime)
                        ORDER BY OrderNumber DESC";
           List<EOrder> list = null;
           try
           {
               using (SqlCommand cmd = new SqlCommand(sql, Con))
               {
                   DateTime yesterday=DateTime.Now.Date;
                   DateTime tomorrow=DateTime.Now.AddDays(1).Date;
                   cmd.Parameters.Add(new SqlParameter("@shopid", ShopId));
                   cmd.Parameters.Add(new SqlParameter("@starttime", yesterday));
                   cmd.Parameters.Add(new SqlParameter("@endtime", tomorrow));
                   DbOpen(); 
                   EOrder order = null;
                   using (IDataReader dr = cmd.ExecuteReader())
                   {
                       list = new List<EOrder>();
                       while (dr.Read())
                       {
                           order = new EOrder();
                           Guid orderId=new Guid(dr["OrderId"].TString());
                           order.OrderId = orderId;
                           Guid userId = new Guid(dr["UserId"].TString());
                           order.UserId = userId;
                           order.OrderName = dr["OrderName"].TString();
                           order.OrderAddress = dr["OrderAddress"].TString();
                           order.OrderMobile = dr["OrderMobile"].TString();
                           order.TotalPrice = dr["TotalPrice"].TDecimal();
                           order.EarliestTime = dr["EarliestTime"].TDateTime();
                           order.LatestTime = dr["LatestTime"].TDateTime();
                           order.OrderType =dr["OrderType"].TInt32();
                           order.OrderNumber=dr["OrderNumber"].TInt32();
                           list.Add(order);
                       }
                   }
                   return list;
               }
           }
           catch (Exception ex)
           {
               base.WriteLog("DALShopOrder_GetTodayOrderList" + ex.Message);
               return null;
           }
           finally
           {
               DbClose();
           }
       }

       public IList<EOrder> GetHistoryOrderList(Guid ShopId)
       {
           String sql = @"SELECT OrderId,UserId,OrderAddress,OrderMobile,TotalPrice,EarliestTime,LatestTime,OrderName,OrderType,OrderNumber
                            FROM Fct_Order 
                           WHERE ShopId=@shopid AND Rec_CreateTime<@starttime
                        ORDER BY OrderNumber DESC";
           List<EOrder> list = null;
           try
           {
               using (SqlCommand cmd = new SqlCommand(sql, Con))
               {
                   DateTime yesterday = DateTime.Now.Date;
                   cmd.Parameters.Add(new SqlParameter("@shopid", ShopId));
                   cmd.Parameters.Add(new SqlParameter("@starttime", yesterday));
                   DbOpen();
                   EOrder order = null;
                   using (IDataReader dr = cmd.ExecuteReader())
                   {
                       list = new List<EOrder>();
                       while (dr.Read())
                       {
                           order = new EOrder();
                           Guid orderId = new Guid(dr["OrderId"].TString());
                           order.OrderId = orderId;
                           Guid userId = new Guid(dr["UserId"].TString());
                           order.UserId = userId;
                           order.OrderName = dr["OrderName"].TString();
                           order.OrderAddress = dr["OrderAddress"].TString();
                           order.OrderMobile = dr["OrderMobile"].TString();
                           order.TotalPrice = dr["TotalPrice"].TDecimal();
                           order.EarliestTime = dr["EarliestTime"].TDateTime();
                           order.LatestTime = dr["LatestTime"].TDateTime();
                           order.OrderType = dr["OrderType"].TInt32();
                           order.OrderNumber = dr["OrderNumber"].TInt32();
                           list.Add(order);
                       }
                   }
                   return list;
               }
           }
           catch (Exception ex)
           {
               base.WriteLog("DALShopOrder_GetHistoryOrderList" + ex.Message);
               return null;
           }
           finally
           {
               DbClose();
           }
       }

       public IList<EOrderDetails> GetOrderDetails(Guid OrderId)
       {
           String sql = @"SELECT f.CommodityName,
                                 a.CommodityId,
                                 a.Amount,
                                 a.UnitPrice,
                                 a.TotalPrice,
                                 a.Discount,
                                 a.DiscountPrice,
                                 a.ActualTotalPrice,
                                 g.CommodityAccessoriesName,
                                 a.CommodityAccessoriesPrice,
                                 c.AttributeDetailsName,
                                 e.SpecificationName
                            FROM Rel_OrderDetails a
                      INNER JOIN Rel_OrderDetailsAttribute b ON a.OrderDetailsId=b.OrderDetailsId
                      INNER JOIN Rel_AttributeDetails c ON b.AttributeDetailsId=c.AttributeDetailsId
                      INNER JOIN Rel_CommodityPrice d ON a.CommodityPriceId=d.CommodityPriceId
                      INNER JOIN Dim_Specification e ON d.SpecificationId=e.SpecificationId
                      INNER JOIN Fct_Commodity f ON a.CommodityId=f.CommodityId
                       LEFT JOIN Rel_CommodityAccessories g ON a.CommodityAccessoriesId=g.CommodityAccessoriesId
                           WHERE a.OrderId=@orderid";
           List<EOrderDetails> list = null;
           try
           {
               using (SqlCommand cmd = new SqlCommand(sql, Con))
               {
                   cmd.Parameters.Add(new SqlParameter("@orderid", OrderId));
                   DbOpen();
                   using (IDataReader dr = cmd.ExecuteReader())
                   {
                       list = new List<EOrderDetails>();
                       EOrderDetails orderDetails = null;
                       while (dr.Read())
                       {
                           orderDetails = new EOrderDetails();
                           Guid commodityId = new Guid(dr["CommodityId"].TString());
                           orderDetails.CommodityId = commodityId;
                           orderDetails.CommodityName = dr["CommodityName"].TString();
                           orderDetails.Amount = dr["Amount"].TInt32();
                           orderDetails.UnitPrice = dr["UnitPrice"].TDecimal();
                           orderDetails.TotalPrice = dr["TotalPrice"].TDecimal();
                           orderDetails.Discount = dr["Discount"].TDecimal();
                           orderDetails.DiscountPrice = dr["DiscountPrice"].TDecimal();
                           orderDetails.ActualTotalPrice = dr["ActualTotalPrice"].TDecimal();
                           orderDetails.CommodityAccessoriesName = dr["CommodityAccessoriesName"].TString();
                           orderDetails.CommodityAccessoriesPrice = dr["CommodityAccessoriesPrice"].TDecimal();
                           orderDetails.AttributeDetailsName = dr["AttributeDetailsName"].TString();
                           orderDetails.SpecificationName = dr["SpecificationName"].TString();
                           list.Add(orderDetails);
                       }
                   }
                   return list;
               }
           }
           catch (Exception ex)
           {
               base.WriteLog("DALShopOrder_GetOrderDetailst" + ex.Message);
               return null;
           }
           finally
           {
               DbClose();
           }
       }

       /// <summary>
       /// 查看是否有新订单
       /// </summary>
       /// <param name="orderNumber">订单自动编号</param>
       /// <param name="ShopId"></param>
       /// <returns></returns>
       public bool HasNewOrder(string orderNumber, Guid ShopId)
       {
           String sql = @"SELECT Max(OrderNumber)
                            FROM Fct_Order 
                           WHERE ShopId=@shopid AND (Rec_CreateTime between @starttime AND @endtime)";
           try
           {
               DateTime yesterday = DateTime.Now.Date;
               DateTime tomorrow = DateTime.Now.AddDays(1).Date;
               List<SqlParameter> list = new List<SqlParameter>();
               list.Add(new SqlParameter("@shopid", ShopId));
               list.Add(new SqlParameter("@starttime", yesterday));
               list.Add(new SqlParameter("@endtime", tomorrow));

               int result = DBHelperSQL.sqlExecuteScalar(sql,list).TInt32();
               if (result > orderNumber.TInt32())
               {
                   return true;
               }
               else
               {
                   return false;
               }
           }
           catch (Exception ex)
           {
               base.WriteLog("DALShopOrder_GetTodayOrderList" + ex.Message);
               return false;
           }
           finally
           {
               DbClose();
           }
       }
   }
}
